home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 6: Level 6 / 17 Bit - Level 6 (1998)(Epic Marketing)[!].iso / quartz / q0970.dms / q0970.adf / basic.doc < prev    next >
Text File  |  1992-09-02  |  36KB  |  1,070 lines

  1. FESBasic/basic.doc
  2.  
  3. CONTENTS
  4. ========
  5.  
  6.     1.  Overview
  7.  
  8.     2.  Expressions
  9.  
  10.     3.  Control Constructs
  11.  
  12.     4.  Statements
  13.  
  14.     5.  Final Points
  15.  
  16.  
  17. ************************************************************************
  18. ========================================================================
  19.                                 1 Overview
  20. ========================================================================
  21. ************************************************************************
  22.  
  23. 1.1 Preamble
  24. ============
  25.  
  26. This file contains a description of the FESBasic Programming language.
  27. I have tried to be as complete as possible in my description of the
  28. language as I could in this file so I must apologize if in some places
  29. I appear to be stating the obvious, and in others going into excessive
  30. technicalities.
  31.  
  32. I suggest that this file be browsed rather than read. Anyone with a
  33. moderate knowledge of BASIC should probably skip to section 5 then look
  34. at the example files and use the on-line help facilities, then refer to
  35. this file only when absolutely necessary.
  36.  
  37. Persons with little or no previous knowledge of computer programming
  38. will probably find this document too "technical" to be of much use. The
  39. best advice I can give is too find a friend who knows about BASIC, then
  40. play around with this program, contacting your friend when ever you are
  41. stuck.
  42.  
  43. By the way the HelpFile can be browsed through by just double-clicking
  44. its icon. It consists of a list of all the commands and functions in
  45. FESBasic along with descriptions of each one's usage.
  46.  
  47. 1.2 The Language
  48. ================
  49.  
  50. FESBasic is a modern version of the BASIC Language. Like most modern
  51. BASICs there are no line numbers, so commands like GOTO are rarely used.
  52.  
  53. Without line numbers control constructs like FOR...NEXT and DO...LOOP take
  54. a much higher importance. As do Procedures and user defined functions. FES
  55. BASIC offers  rich choice of these to the programmer, allowing the creation
  56. of well structured programs. In addition the editor automatically formats
  57. your programs to give them that 'structured' indented look.
  58.  
  59. It must be pointed out at this stage that work is still continuing on the
  60. Language. See the "Register.doc" file for more information about getting
  61. hold of the latest version of the language.
  62.  
  63. Probably the best way to start this file is with an example:-
  64.  
  65. 'FES BASIC EXAMPLE PROGRAM
  66. 'Doesn't do much, but what do you expect?
  67.  
  68. FOR k=1 to 10
  69.   COLOR k   'Comments can be placed on any line
  70.   PROC Put_Text "Line %d",k
  71. NEXT
  72. STOP
  73.  
  74. DEFPROC Put_Text text$,number
  75.   LOCAL a$
  76.   a$=FORMAT$(text$,number)
  77.   PRINT a$
  78. ENDPROC
  79.  
  80. <<<THE END>>>
  81.  
  82. That should give me enough to talk about for a few paragraphs.
  83.  
  84. Comments can be placed on any line. A comment is signalled by a '
  85. and continues until the end of the line.
  86.  
  87. The editor automatically indents your code for you.
  88.  
  89. Your programs automatically get their own screen for their output.
  90.  
  91.  
  92. *************************************************************************
  93. =========================================================================
  94.                             2 EXPRESSIONS
  95. =========================================================================
  96. *************************************************************************
  97.  
  98. An expression can be used almost anywhere a value is required in FESBasic.
  99.  
  100. An expression can consist of:
  101.     Constants   eg    1    -4     "hello"
  102.     Variables   eg    a   hello&  MyString$
  103.     Operators   eg    +  -  *  /  =  <  <= AND OR
  104.     Functions   eg    RND VAL STR$ INC AND
  105.     Brackets          ( )
  106.     UserFuncs   eg    FNxyz  FNabc$
  107.  
  108. Lets consider each of these in turn.
  109.  
  110. 2.1 Constants
  111. =============
  112.  
  113. There are three types of constant: int , long and string.
  114.  
  115. An int constant consists of an optional minus sign followed by up to 5 digits
  116. (0123456789). An int constant must be in the range -32768 to 32767.
  117.  
  118. A long constant is like an int but it is followed by a & (eg 65538& ), unlike
  119. int constants a long can be any size between -4294967296 and 4294967295. If
  120. you type a number too big for an int then the editor will automatically add
  121. the & to make a long. Arithmetic involving longs is slower than that involving
  122. only ints.
  123.  
  124. A string constant is started by a " and is terminated by the same.
  125.  
  126. 2.2 Variables
  127. =============
  128.  
  129. Like constants a variable can be any of three types, int long or string.
  130.  
  131. Int variables consist of a letter followed by 0 to 18 alphanumeric symbols,
  132. the underscore _ and the 'at' symbol @ are considered to be letters for this
  133. definition. So valid names are
  134.     x
  135.     abc
  136.     Hello
  137.     Hi_There
  138.     @
  139.  
  140. But invalid names include:
  141.     3_blind_mice        as it starts with a digit
  142.     a_very_long_name_that never_seems_to_end     as it's >19 characters
  143.     a+b                 it contains a +
  144.  
  145. Int variables store only whole numbers in the range -32768 to 32767.
  146.  
  147. Long variables have similar names to ints but they must end with a &. This
  148. is included in the 18 alphanumeric symbols, again the total name cannot be
  149. more than 19 characters. A long can store whole values in the range
  150. -4294967296 to 4294967295.
  151.  
  152. As you have probably guessed by now a string variable name consists of the
  153. same restrictions as an int but must end in a $
  154. A string variable can store up to 100000 characters of text.
  155.  
  156.  
  157. A value is placed in a variable by an assignment. This takes the form
  158.  
  159. variable_name = expression
  160.  
  161. on a line of its own. 'expression' must be of the same type as variable_name
  162. (ie both ints or both strings), but some flexibility is allowed with ints and
  163. longs, if a conversion is possible then one will be made, so it is acceptable
  164. to write
  165.  
  166. a&=5
  167.  
  168. despite a& being a long variable and 5 being an int constant.
  169.  
  170. 2.3 Operators
  171. =============
  172.  
  173. Operators combine the value of two sub-expressions (called operands)
  174. relational and boolean. There are also similar operators for strings.
  175.  
  176. 2.3.1 Arithmetic Operators
  177. --------------------------
  178.  
  179. Arithmetic operators combine two numbers together to form a third. The
  180. returned value will be a long unless both operands are ints when the return
  181. value will be an int.
  182.  
  183. There are 4 Arithmetic Operators
  184.  
  185.     +       Performs Addition of the two operands
  186.     -       Performs Subtraction
  187.     *       Multiplication
  188.     /       Integer Division ( fractions are ignored)
  189.  
  190. When an expression contains both multiplication/division and
  191. addition/subtraction then the Multiply/Divides are done before the add/subs.
  192.  
  193. so  3+5*6  is 33 not 48
  194. and 5+3/4  is 5  NOTE in Integer division 3/4 is zero.
  195.  
  196. To perform additions first use brackets
  197.  
  198. eg (3+5)*6
  199.  
  200.  
  201. 2.3.2 Relational Operators
  202. --------------------------
  203.  
  204. A relational operator compares the values of its two operands and returns a
  205. the int 'one' if some relationship is true, or zero if the relationship is
  206. false.
  207.  
  208. The available relational Operators are
  209.  
  210.     <       Less Than
  211.     >       Greater Than
  212.     =       Equal
  213.     <>      Not Equal
  214.     <=      Less than or equal
  215.     >=      Greater than or equal
  216.  
  217. Relational operations are performed after arithmetic ones , so
  218.  
  219.       5+8<7*3     gives a value 1
  220. and   b*a<>b*b    gives 1 unless 'a' and 'b' are equal.
  221.  
  222. 2.3.3 Boolean Operators
  223. -----------------------
  224.  
  225. A boolean operator tests its two operators for "truth" (true being defined
  226. as any value except zero) and gives a value based on the "truths" of both.
  227.  
  228. The three Boolean Operators are
  229.  
  230.     AND     Gives '1' if and only if BOTH are true, else gives '0'
  231.     OR      Gives '1' if either or both are true , else 0
  232.     XOR     Gives '1' if one but not both are true, else 0
  233.  
  234. Boolean operators are evaluated after relational ones.
  235.  
  236. NOTE: Unlike some other BASICs FESBasic's operators are 'logical' not
  237. 'bitwise'. ie   5 AND 4  is 1 not 4.
  238.  
  239. 2.3.4 String Operators
  240. ----------------------
  241.  
  242. There are less operators that can be applied to strings than to numbers.
  243.  
  244. For 'Arithmetic' operators the only one available is 'concatenation' +
  245. This operator joins two strings together, ie "egg and "+"chips" gives
  246. "egg and chips"
  247.  
  248. All the relational operators can be applied. For comparison a one string
  249. is less than another if its first letter's ASCII code is less than the
  250. second strings first letter. If the first letters are the same then the
  251. comparison is made on the second letter, then the end of one string is
  252. reached.
  253.  
  254. So the following relationships are all true:-
  255.  
  256.     "chips" < "egg"         c comes before e
  257.     "Egg" < "chips"         Capitals come before lower-case
  258.     "2Chips" < "egg"        Numbers come before letters
  259.     " Egg" < "chips"        Spaces come before letters.
  260.     "chips"<>"CHIPS"        The case's are different so not equal
  261.     "chips" = "chips"       Identical.
  262.  
  263. Most basics do not allow Boolean operations on strings. FESBasic contains
  264. an extension to the language to allow:-
  265.  
  266.     string AND number   gives   string if number is 'true'
  267.                                  ""    if number is 'false'
  268.  
  269.     string1 OR string2  gives   string1 if string1<>""
  270.                                 string2 if string1=""
  271.  
  272. These can be occasionally useful:
  273.  
  274. OTHER BASIC:    c$=a$
  275.                 IF a$="" then c$=b$
  276. FES BASIC:       c$=a$ OR b$
  277.  
  278. OTHER BASIC:    PRINT "There are ";n;" cat";
  279.                 IF n<>1 THEN PRINT "s";
  280.                 PRINT " on the roof"
  281. FES BASIC:      PRINT "There are ";n;"s" AND n<>1;" on the roof"
  282.  
  283.  
  284.  
  285. 2.4 Functions
  286. =============
  287.  
  288. A function takes zero or more values (called parameters) and produces a
  289. single value as a result (known as the return value).
  290.  
  291. If a function takes two or more parameters then the function name must be
  292. followed by a '(' and each of the parameters must be separated by commas
  293. so for example BTST(a,3).
  294.  
  295. For a function with just one parameter the brackets are optional, so it is
  296. acceptable to write ABS(x) or ABS x
  297.  
  298. For functions with no parameters there must not be brackets, eg TIMER
  299.  
  300. Each individual function specifies how many parameters it takes and of what
  301. type each must be. It is an error to provide the wrong number or types, and
  302. the computer will give an error message, either when the program is executed
  303. or as the line is entered.
  304.  
  305. Functions are evaluated BEFORE operators so     ABS a+b
  306. means (ABS a)+b   not    ABS(a+b).
  307.  
  308. For a complete list of the functions available in FESBasic refer to the
  309. "FESBasic_HelpFile", where they are all listed alphabetically with descriptions.
  310.  
  311. I will now briefly describe the more interesting/useful functions:-
  312.  
  313.  
  314. 2.4.1 String Manipulation
  315. -------------------------
  316.  
  317. If we have a string ( for example h$ or "hello") we may need to split it up
  318. into smaller pieces. All BASICs provide functions to do this.
  319.  
  320. The most general one is MID$. This takes as parameters the string to be
  321. sliced, a position to start and a length. It then returns a string made up of
  322. a part of the initial string.
  323.  
  324. so  MID$("Hello",2,3)   gives "ell"     e being the 2nd letter and continuing
  325.                                         for 3 characters.
  326.  
  327. If the second number is left out then the string continues to the end of the
  328. first string, ie  MID$("Hi There",4)  is "There"
  329.  
  330. FESBasic also allows the second number to be specified as a position, so you
  331. can write MID$("Egg and Chips",5 TO 7)  to get "and".
  332.  
  333. Other available functions are LEFT$(a$,n) which gives the first 'n' characters
  334. of a$, and RIGHT$(a$,n) which gives the last 'n' characters.
  335.  
  336. To convert the case of a string you can use UCASE$(a$) which converts all
  337. small letters in string to capitals, and LCASE$ which is the opposite.
  338.  
  339. Finally the function TRIM$ removes any spaces from the beginning and end of a
  340. string, so TRIM$("  hello  ") gives "hello"
  341.  
  342. 2.4.2 Bit Manipulation
  343. ----------------------
  344.  
  345. These functions are only needed by more experienced programmers.
  346.  
  347. You may have noticed that the AND,OR and XOR operators in FESBasic
  348. are 'logical' not bitwise. The bitwise operations are available but
  349. as functions. So to mask out bits except 0 and 1 of 'a' use
  350.                 a=AND(a,3)
  351.  
  352. Also direct bit manipulation functions are available:
  353.     BSET(a,n)   sets bit n of a
  354.     BCLR(a,n)   clears bit n of a
  355.     BCHG(a,n)   toggles bit n of a
  356.     BTST(a,n)   tests bit n of a
  357.  
  358. Note these are functions not statements, so write a=BCLR(a,0)
  359. instead of BCLR a,0
  360.  
  361. Also note that NOT is a logical function. To do a bitwise negation use
  362. something like XOR(n,-1)
  363.  
  364. 2.4.3 Arithmetic
  365. ----------------
  366.  
  367. Various mathematical operations are available:-
  368.     ABS n       the "absolute value" of n, ie n made positive
  369.     MOD(a,b)    the remainder when 'a' is divided by 'b'
  370.     -           negation
  371.     INC n       n+1
  372.     DEC n       n-1
  373.     MULT(a,b)   a*b
  374.     DIV(a,b)    a/b
  375.  
  376. The top three of these are standard BASIC, the last 4 are new to FES.
  377.  
  378. MULT and DIV are subtly different to the * and / operators. All work is
  379. performed in 16 bits, with no error checking performed. Division by
  380. zero gives int machine infinity, (ie 32767). Multiplication results are
  381. modulo 65536, sign adjusted. If this does not mean much to you then you
  382. will probably never need to use these functions.
  383.  
  384. INC and DEC can be useful in avoiding brackets sometimes, 3*INC a instead
  385. of 3*(a+1). It is a matter of personal choice really. There is little
  386. performance difference between either form.
  387.  
  388. 2.4.4 Input/Output Functions
  389. ----------------------------
  390.  
  391. A range of functions are available to perform input and output operations:-
  392.  
  393.     INKEY         Key Press as ASCII code
  394.     INKEY$        Key Press as string
  395.     SHIFTKEY      Which Qualifier keys are pressed
  396.     INPUT$(a)     Read 'a' characters from keyboard
  397.     INPUT$(#n,a)  Read 'a' characters from a file.
  398.     MOUSEB        Which Mouse Buttons are pressed
  399.     MOUSEX        Mouse X position
  400.     MOUSEY        Mouse Y position
  401.     STICKB(n)     Joystick 'n's Fire Button
  402.     STICKX(n)     Joystick 'n's Left/Right Position
  403.     STICKY(n)     Joystick 'n's Up/Down Position
  404.  
  405. 2.4.5 Format Conversion Functions
  406. ---------------------------------
  407.  
  408. It is often required to convert data from one format to another.
  409.  
  410. To convert an int or a long to a string of digits use STR$. To perform the
  411. reverse operation, a string of digits to a long, use VAL.
  412.  
  413. To convert an int/long to an ASCII string use CHR$, the reverse can be
  414. done with ASC.
  415.  
  416. Ints and Longs can be converted to 2 or 4 character strings respectively
  417. by using MKI$ or MKL$. These are different from STR$ in that STR$
  418. produces a string that makes sense to a human, eg STR$(84) is "84".
  419. MKI$(84) is "_T". To convert back from the MK?$ format use CVI or CVL.
  420.  
  421. CVI and CVL can also be used to convert ints to longs or vice versa.
  422. Although this is normally done automatically it is sometimes necessary to
  423. do it explicitly. For example
  424.   PRINT 3000*9000       gives an overflow error, as it multiplies two ints
  425.                         to give an int, but the answer is too big for an
  426.                         int to cope with, but
  427.   PRINT 3000*CVL 9000   does work, as 9000 is now a long, so the result of
  428.                         the multiplication is a long, which can cope with
  429.                         the value 27000000. Note if, as in this case, you
  430.                         are working with constants, you could use
  431.   PRINT 3000*9000&      which is more succinct.
  432.  
  433. {When CVI or CVL are used like this it is jargon to refer to them as
  434.  "cast" operators. That's a good one to impress your friends with your
  435.  knowledge of computing}
  436.  
  437. 2.5 User Defined Functions
  438. ==========================
  439.  
  440. FESBasic allows you to build up functions of your own. All User Defined
  441. Functions must begin with the letters 'FN' followed by a name, the
  442. rules for FN names are the same as for variable names, so valid
  443. names are
  444.  
  445.     FNadd           FNSwap$         FNlong_var_name&
  446.  
  447. When it is used a user defined function must be followed by brackets. Even
  448. if there are no augments the brackets must still be specified.
  449.  
  450. Functions are defined using DEF. The end of the function definition is
  451. marked by a ENDDEF statement followed by an expression. This expression gives
  452. the value returned by the function.
  453.  
  454. FES Basic does allow recursion. That is a function can be defined in terms
  455. of itself. So the standard example; the Factorial:-
  456.  
  457.     'Factorial Program
  458.     'Asks for a number then calculates its factorial
  459.     '{ So if the user gives the number 5 the answer is 1x2x3x4x5 }
  460.  
  461.     INPUT "Give me a number ",a
  462.     PRINT "Factorial of ";a;" is ";FNFactorial&(a)
  463.     STOP
  464.  
  465.     DEF FNFactorial& n          'use & as numbers can be quite big
  466.       ' Factorial calculated recursively.
  467.       ' Factorial 1 =1;
  468.       ' Factorial n = n* Factorial (n-1)
  469.  
  470.       IF n=1 THEN RETURN 1      'RETURN can be used to return value
  471.  
  472.     ENDDEF n*FNFactorial&(n-1)   'call FNFactorial again, and again...
  473.  
  474.  
  475. Notice the slightly weird syntax for DEF. There are no brackets around
  476. the list of augments. (It is the same for DEFPROC later). I'm not quite
  477. sure why I programmed it that way, but that's the way things happened!
  478.  
  479. Function definitions can contain LOCAL variables, just like PROCedures.
  480.  
  481.  
  482.  
  483. **************************************************************************
  484. ==========================================================================
  485.                             3 CONTROL CONSTRUCTS
  486. ==========================================================================
  487. **************************************************************************
  488.  
  489. Probably the most important parts of computer programs are the statements
  490. which control the flow of the programs. Performing calculations is fine
  491. but if that is all that is required then the end-user is probably better
  492. off with a pocket calculator. The main advantage of a computer is its
  493. ability to perform operations many times, this looping constructs.
  494.  
  495. There are several Constructs that can be used in FES Basic to perform
  496. branching and looping.
  497.  
  498.     Labels:...GOTO
  499.     IF...[THEN]
  500.     FOR...NEXT
  501.     REPEAT...UNTIL
  502.     WHILE...WEND
  503.     DO...LOOP
  504.     SELECT...CASE
  505.     PROC
  506.  
  507. There is also the EXIT command which can be used to make an early exit from
  508. a FOR...NEXT, DO...LOOP ,WHILE...WEND or REPEAT...UNTIL.
  509.  
  510. 3.1 Labels:...GOTO
  511. ==================
  512.  
  513. These are the least preferred way to perform operations. This method is
  514. very much frowned upon in professional programming circles, however it
  515. possibly the one that beginners may find themselves most familiar with.
  516.  
  517. A Label is a method of marking a point in a program. In FESBasic this is
  518. done by placing an Alphanumeric word at the beginning of a line followed
  519. by a :.
  520.  
  521. The computer can be made to jump to a label at any point by the use of the
  522. GOTO command. The word GOTO is followed by the label of the point to jump
  523. to. There must not be a : after the label at the GOTO.
  524.  
  525. For example, the standard program that everyone writes:-
  526.  
  527.         Label:
  528.         PRINT "FOZZ is ace!"
  529.         GOTO Label
  530.  
  531. Care must be taken with GOTO's not to jump into or out of other control
  532. constructs. If you do the behaviour is not defined. There may be an error
  533. message, the program may work correctly on some computers but not on others,
  534. etc. Sod's law states that in this last case then the only computer in the
  535. cosmos that the program works on will be the programmers own when no-one
  536. else is in the room.
  537.  
  538. Although the GOTO command is available it should only be used where absolutely
  539. necessary. It is much preferred to use other constructs:-
  540.  
  541.         DO
  542.           PRINT "FOZZ is ace!"
  543.         LOOP
  544.  
  545. 3.2 IF...[THEN]
  546. ===============
  547.  
  548. This is the main decision making construct. It can take two forms:-
  549.  
  550.     IF condition THEN statement
  551.  
  552. or  IF condition
  553.       statement
  554.       statement
  555.       :
  556.       :
  557.     ENDIF
  558.  
  559. The first form is quicker to type, requires less lines, but can only cope
  560. with one statement. The second form can cope with any number of statements,
  561. even with more IF statements.
  562.  
  563. What forms a "condition"?
  564.  
  565. It can in fact be any expression that gives a numeric result. If the result
  566. is not zero then it is regarded as "true",if zero then "false".
  567.  
  568. So we can say:-
  569.     IF a<b THEN PRINT "a is less than b"
  570. or  IF a=2 OR a=3 THEN PRINT "2 or 3"
  571.  
  572. {NOTE it is not possible to write
  573.     IF a=2 OR 3 THEN PRINT "2 or 3"
  574.  See section 2.3.3 for what this means! }
  575.  
  576. If we are using the longer IF construct then we can have several conditions,
  577. these are of the form ELSEIF condition. This allows the use of multi-way
  578. decisions:-
  579.  
  580.     IF a=1
  581.         PRINT "MONO-"
  582.     ELSEIF a=2
  583.         PRINT "BI-"
  584.     ELSE
  585.         PRINT "MULTI-"
  586.     ENDIF
  587.  
  588. There can be any number of ELSEIF clauses, and each block of statements can
  589. contain any number of statements, but in all cases only one block will get
  590. executed.
  591.  
  592. 3.3 FOR...NEXT
  593. ==============
  594.  
  595. The FOR...NEXT construct is used to repeat a section of the program a given
  596. number of times.
  597.  
  598. A variable is used as a counter, It is set to a value at the start of the
  599. loop, then increased by a given value on each loop until it reaches another
  600. given value.
  601.  
  602.     ' eg count from 1 to 10
  603.     FOR k=1 TO 10
  604.       PRINT k
  605.     NEXT
  606.  
  607. If it is required to count down then either use STEP with a negative value
  608. or FOR val=value DOWNTO value2.
  609.  
  610. 3.4 REPEAT...UNTIL
  611. ==================
  612.  
  613. REPEAT...UNTIL does exactly that. The block of program between the REPEAT and
  614. the UNTIL is executed repeatedly until a condition is satisfied.
  615.  
  616.     PRINT "Enter 0 to exit"
  617.     REPEAT
  618.       INPUT "Give me a number ";a
  619.       PRINT a;" Squared is ";a*a
  620.     UNTIL a=0
  621.  
  622. Note that when using REPEAT...UNTIL the block is executed at least once.
  623.  
  624. 3.5 WHILE...WEND
  625. ================
  626.  
  627. This is the mirror of REPEAT...UNTIL. Unlike REPEAT the condition is placed
  628. at the beginning of the block. The block is terminated with a WEND instruction.
  629.  
  630. This allows a block to be executed zero times if the condition fails the
  631. first time round.
  632.  
  633.     PRINT "Exponential Tables :"
  634.     INPUT "Give me a number ";a
  635.     WHILE a<10000
  636.       PRINT a
  637.       a=a*a
  638.     WEND
  639.  
  640. 3.6 DO...LOOP
  641. =============
  642.  
  643. This construct is a sort of combined WHILE/REPEAT. The loop repeats
  644. continuously until an EXIT command is executed.
  645.  
  646.     PRINT "Enter 0 to stop"
  647.     DO
  648.       INPUT "Give me a number ",a
  649.       EXIT a=0
  650.       PRINT a;" squared is ";a*a
  651.     LOOP
  652.     PRINT "Finished"
  653.  
  654. 3.7 EXIT
  655. ========
  656.  
  657. The Exit command can be used to leave a loop part of the way through. It is
  658. the only way to leave a DO...LOOP, and can be used to exit from any of the
  659. other three loops.
  660.  
  661. The simplest form of the command is
  662.  
  663.     EXIT type_of_loop
  664.  
  665. eg  EXIT
  666.     EXIT FOR
  667.     EXIT REPEAT
  668.     EXIT WHILE
  669.  
  670. Exit on its own is to exit from a DO...LOOP.
  671.  
  672. In addition a condition may be placed after the "type", in this case the loop
  673. will only exit if the condition is true.
  674. NOTE this looks weird when used on a WHILE loop, eg
  675.     EXIT WHILE a<3      means exit the loop if a is less than 3.
  676.  
  677.  
  678. 3.8 SELECT...CASE...
  679. ====================
  680.  
  681. This allows a multi-way decision.
  682.  
  683. After the SELECT comes an expression. The computer then searches through
  684. the list of CASE's for a match. If one is found then the block after that
  685. case is executed. The word REMAINDER can be used to catch any cases not
  686. matched before.
  687.  
  688.     PRINT "Press:-"
  689.     PRINT |"1.  Load"||"2. Save"||"3. Exit"
  690.     INPUT ">",action
  691.  
  692.     SELECT action
  693.     CASE 1
  694.       INPUT "Load which File? ",FName$
  695.       PROC LoadFile FName$
  696.     CASE 2
  697.       INPUT "Save which file? ",FName$
  698.       PROC SaveFile FName$
  699.     CASE 3
  700.       STOP
  701.     CASE REMAINDER
  702.       PRINT "Invalid Option"
  703.     END SELECT
  704.  
  705.  
  706. 3.9 PROCedures
  707. ==============
  708.  
  709. Procedures are the recommended way of splitting a program up into manageable
  710. chunks. They allow blocks of program to be named, each block can have its own
  711. 'local' variables separate from all other blocks, and generally makes your
  712. programs far better structured.
  713.  
  714. Unfortunately the syntax for PROCedures in FES Basic is highly non-standard.
  715. I'm not sure how the syntax came about, but I'm stuck with it now. Its not to
  716. bad once you get used to it.
  717.  
  718. Lets start of by looking at how to define a procedure. First comes the
  719. statement DEFPROC then the procedure name (alpha numeric just like a variable)
  720. then a space then any parameters it takes, all separated by commas.
  721.  
  722. eg      DEFPROC myproc a&,b,c$
  723.  
  724. Note that the variables used in a DEFPROC line are independent of any
  725. variables that may have the same name in the rest of the program.
  726.  
  727. After the DEFPROC comes the program lines that make up the procedure, and
  728. the whole thing is finished of with an ENDPROC.
  729.  
  730. So a full example:
  731.  
  732.         DEFPROC stars n_spaces,n_stars
  733.           LOCAL k       'the variable 'k' is independent of any other 'k's
  734.  
  735.           FOR k=1 to n_spaces
  736.             PRINT " ";
  737.           NEXT
  738.  
  739.           FOR k=1 to n
  740.             PRINT "*";
  741.           NEXT
  742.           PRINT
  743.         ENDPROC
  744.  
  745. To run the procedure use the command PROC, followed by the name then the
  746. values to give each parameter. So to display 5 spaces then 3 stars in the
  747. above example use
  748.  
  749.         PROC stars 5,3
  750.  
  751. Either value could have been a variable or a complete expression.
  752.  
  753. See the example file "procs.fes" for a complete example.
  754.  
  755.  
  756.  
  757.  
  758. ****************************************************************************
  759. ============================================================================
  760.                             4. STATEMENTS
  761. ============================================================================
  762. ****************************************************************************
  763.  
  764. A 'statement' is the word at the beginning of each line which describes what
  765. that line is going to do. So PRINT,GOTO,IF,LINE,CLS,COLOR are all statements.
  766.  
  767. FESBasic currently contains about 65 statements (There is some confusion about
  768. lines containing a comment only, or about assignment lines, but this is
  769. all besides the point!).
  770.  
  771. For a full list of all the statements please refer to the FESBasic_HelpFile.
  772. It consists off all the statements with descriptions.
  773.  
  774. In this section I will describe very briefly some of these statements by
  775. subjects...
  776.  
  777. 4.1 PROGRAM FLOW
  778. ================
  779.  
  780. There are quite a few statements which control the flow of execution around
  781. a program. These are explained in more detail in section 3: Control Constructs
  782.  
  783. GOTO label      The most blatant. Causes a jump to a specified point.
  784.  
  785. GOSUB label     Causes a jump to the label. When a RETURN statement is
  786. RETURN          encountered the program jumps back to the line after the
  787.                 GOSUB.
  788.  
  789. PROC            This causes a jump to be made to a given location, like GOSUB
  790. DEFPROC         however PROC allows passing of parameters, and provides
  791. ENDPROC         clearly defined limits to blocks of program.
  792.  
  793. FOR             This is used to repeat a number of lines a set number of
  794. NEXT            times. Use this construct when you can determine the number
  795.                 of times round the loop before you start.
  796.  
  797. REPEAT          This is used to repeat a block of program several times until
  798. UNTIL           a given condition is met. The block will always be executed
  799.                 at least once.
  800.  
  801. WHILE           Similar to REPEAT...UNTIL but the condition is at the start
  802. WEND            of the block. This means that if the condition fails on the
  803.                 first time round then no code will be executed.
  804.  
  805. DO              Repeats a block of code indefinatly until an EXIT statement
  806. LOOP
  807.  
  808. EXIT            Leave a looping construct
  809.  
  810. IF              Allows decision making, a block of code can be executed only
  811. ELSEIF          if certain conditions are met.
  812. ELSE
  813. ENDIF
  814.  
  815. SELECT          A more compact form of IF...ELSEIF...ELSEIF...ENDIF when the
  816. CASE            conditions are all very similar.
  817. ENDSELECT
  818.  
  819.  
  820. 4.2 INPUT AND OUTPUT
  821. ====================
  822.  
  823. PRINT           Displays the values of zero or more expressions on the screen.
  824.                 Expressions can be separated by semi-colons in which case they
  825.                 will be displayed side by side on the screen, by commas when
  826.                 they will be displayed in separate columns or by bars '|'
  827.                 when they will appear on separate lines.
  828.  
  829. INPUT           Displays a prompt then accepts information from the user and
  830.                 places it in a variable.
  831.  
  832. OPEN            Opens a channel to a (disk) file. Information can be sent to
  833.                 the channel using PRINT # or WRITE #. Information can be read
  834.                 using INPUT #,  INPUT$(#n) or READ #.
  835.  
  836. CLOSE           Closes a channel opened by OPEN.
  837.  
  838. PRINT #n,       Similar to PRINT but writes the output to channel 'n'
  839.  
  840. WRITE #n,       Writes values to channel 'n' in a computer readable format.
  841.                 This format is understood by READ #
  842.  
  843. INPUT #n,       Similar to INPUT but reads from a file not the keyboard.
  844.  
  845. READ #n,        Retrieves information from a file written to by WRITE #n.
  846.  
  847. BWRITE          Writes a block of memory to a channel opened with OPEN.
  848. BREAD           Reads a block of memory from a channel.
  849.  
  850.  
  851. 4.3 GRAPHICS
  852. ============
  853.  
  854. SCREEN 1,w,h,n  Determines the screen mode that Basic will use. Note the
  855.                 slightly strange syntax with a 1, at the beginning!
  856.  
  857. COLOR f,b,m     Chooses the colour to draw in.
  858.  
  859. PALETTE         Changes the selection of colours available
  860.  
  861. LINE            Draws straight lines on the screen.
  862.  
  863. BOX             Draws hollow or filled boxes.
  864.  
  865. CIRCLE          Draws filled/hollow circles/ellipses.
  866.  
  867. AREA            Fills an arbitrary shape with up to 20 vertices.
  868. AREAFILL
  869.  
  870. FLOOD           Fills a shape already drawn on screen.
  871.  
  872. PSET            Sets a pixel on the screen.
  873.  
  874. GET             Copy a section of screen to a buffer
  875.  
  876. PUT             Copy a buffer onto the screen.
  877.  
  878. SCROLL          Move a part of the screen around.
  879.  
  880.  
  881. ***************************************************************************
  882. ===========================================================================
  883.                         5. FINAL POINTS
  884. ===========================================================================
  885. ***************************************************************************
  886.  
  887. This chapter is intended mainly for persons already familiar with other
  888. versions of BASIC. In it I list the peculiarities of my language in
  889. comparison with others.
  890.  
  891. 5.1 Integers Only
  892. =================
  893.  
  894. The freely distributable version of FESBasic is limited to working only
  895. with Integer Numbers (ie numbers with no decimal points). To obtain a
  896. version that supports real-numbers you need to register (see the
  897. accompanying file Register.doc)
  898.  
  899. This limitation means that for example 5/3 is, as far as the  computer is
  900. concerned, equal to 1, as 1 is the largest number of times that 3 can go
  901. into 5.
  902.  
  903. 5.2 Syntax for PROCs and DEF FNs
  904. ================================
  905.  
  906. Procedures are defined by using the command DEFPROC followed by the proc
  907. name and then any augments, without brackets around the list of
  908. augments. Variables local to a procedure are declared by using the
  909. statement 'LOCAL' on the line immediately after the DEFPROC.
  910.  
  911. To call a procedure the word PROC must be used, and again there are no
  912. brackets around the augment list.
  913.  
  914. So for example
  915.  
  916.         PROC Stars n
  917.         .
  918.         .
  919.         .
  920.  
  921.         DEFPROC Stars number
  922.           LOCAL k
  923.            .
  924.            .
  925.            .
  926.         ENDPROC
  927.  
  928. To define a function the syntax is exactly the same as for a procedure
  929. except use the word DEF not DEFPROC. Again there are no brackets around
  930. the augment list.
  931.  
  932. User defined function names must begin with the letters FN followed by at
  933. least one alphanumeric character. The return type of a function is placed
  934. at the end of the name, so FNx returns an integer, but FNy$ returns
  935. a string.
  936.  
  937. When calling a user defined function however the brackets are essential,
  938. even if there are no parameters.
  939.  
  940. The return value expression is placed after the ENDDEF that finishes the
  941. definition. Early exit from a function is possible using RETURN value but
  942. note that it is illegal to exit from within a loop. The function below
  943. will have undefined effects:-
  944.  
  945.         DEF FNFindValue value
  946.  
  947.           FOR k=1 to 10
  948.             IF a(k)=value THEN RETURN k
  949.           NEXT
  950.  
  951.         ENDDEF 0
  952.  
  953. Some other BASICs do allow this sort of early exiting from definitions, but
  954. FES does not. Using this type of construct can get the computer VERY confused!
  955.  
  956. You may use the RETURN statement within block IF and SELECT constructs however,
  957. but not if the IF/SELECT is within a FOR/DO/WHILE/WEND etc.
  958.  
  959. eg      'This is OK
  960.         DEF FNFindValue Value
  961.  
  962.           SELECT Value
  963.           CASE 1,2,3
  964.             RETURN 1
  965.           CASE 4,5,6
  966.             IF RND(3)>2
  967.               RETURN 2
  968.             ELSE
  969.               RETURN 4
  970.             ENDIF
  971.           CASE REMAINDER
  972.             RETURN 0
  973.           ENDSELECT
  974.         ENDDEF
  975.  
  976. 5.3 LOGICAL AND BITWIZE OPERATIONS
  977. ==================================
  978.  
  979. The operators AND OR and XOR in FESBasic are logical operators. That is they
  980. give the value '1' if the answer is true and '0' for false based on the whole
  981. number input, not on each individual bit input.
  982.  
  983. This means that     a=5 AND 4   gives a=1 not 4 as some BASICs might.
  984.  
  985. The Bitwize operations are available as functions though:-
  986.  
  987. So you would write  a=AND(5,4)  to get a=4.
  988.  
  989. Also the operators AND and OR can be used on Strings:-
  990.  
  991.         a$ AND a        gives   a$ if a<>0
  992.                                 "" if a=0
  993.  
  994.         a$ OR b$        gives   a$ if a$<>""
  995.                                 b$ if a$=""
  996.  
  997. These functions are not often used but can be quite useful, for example to
  998. set a default value on a string:
  999.  
  1000. other BASIC     INPUT "Enter your name ",name$
  1001.                 IF name$="" then name$="Anon"
  1002.                 :
  1003.                 :
  1004.  
  1005. FES BASIC       Input "Enter your name ",name$
  1006.                 name$=name$ OR "Anon"
  1007.  
  1008. or to handle plurals
  1009.  
  1010. other Basic     PRINT n;" apple";
  1011.                 IF n<>1 THEN PRINT "s";
  1012.  
  1013. FESBasic        PRINT n;" apple";"s" AND (n<>1);
  1014.  
  1015.  
  1016. 5.4 PRINT USING and FORMAT$
  1017. ===========================
  1018.  
  1019. FESBasic does NOT support the PRINT USING syntax found on many other versions
  1020. of BASIC. Instead the C-Style function FORMAT$ is used.
  1021.  
  1022. FORMAT$ can be used in any context unlike USING which could only be used in
  1023. conjunction with PRINT.
  1024.  
  1025. eg      'print a persons name and age
  1026.         PRINT FROMAT$("Name: %15s  Age %2d",name$,age)
  1027.  
  1028.         would give something like:-
  1029.  
  1030.         Name:     Simon Forey  Age 20
  1031.  
  1032. 5.5 STAND ALONE PROGRAMS
  1033. ========================
  1034.  
  1035. If you have written a program using FESBasic it is not neccary to load it
  1036. into the editor every time you want to run it.
  1037.  
  1038. If you create a program with an Icon then the program can be run by
  1039. double-clicking its icon. Just make sure that the program "basic" is on
  1040. your disk in the same place as the "Tool Type" of the Icon says it is.
  1041.  
  1042. Running programs from the CLI is equally easy. Just make sure that "basic" is
  1043. in your command path ( eg put it in the 'c' directory of your disk) then
  1044. type        basic file_name.fes
  1045.  
  1046. NOTE: For a program to be executable like this it must have been saved using
  1047. SAVE or SAVE PROT from the editor. Programs saved using SAVE ASCII cannot be
  1048. run without loading them back into the editor.
  1049.  
  1050. So suppose you have written a game called "game.fes" and you want to make a
  1051. bootable disk that will boot to the game. You need the following disk
  1052. structure
  1053.  
  1054.         c
  1055.            basic
  1056.            «any other commands used»
  1057.         s
  1058.            startup-sequence
  1059.         libs
  1060.            reqtools.library     OR   } only if you use the FILEREQ$ function
  1061.            asl.library               }
  1062.         game.fes
  1063.  
  1064. the startup-sequence would look something like
  1065.  
  1066.         «any commands you want to run before game»
  1067.         basic game.fes
  1068.         «any commands for after game»
  1069.  
  1070.